﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using com.lover.astd.common.model;
using com.lover.astd.common.config;
using System.Windows.Forms;
using com.lover.astd.common.manager;
using com.lover.astd.common;

namespace com.lover.astd.game.ui.server.impl
{
    /// <summary>
    /// 完成经常需要更新的任务, 如获取服务器时间, 获取用户基本信息等
    /// </summary>
    public class CommonServer : LogicServer
    {
        public CommonServer(MainForm frm)
        {
            _mainForm = frm;
            ServerName = ConfigStrings.S_Global;
            ServerReadableName = ConfigStrings.SR_Global;
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            if (frm.getConfig() == null) return;
            GameConfig conf = getConfig();
            // save Global settings
            if (frm.chk_global_logonAward.Checked)
                conf.setConfig(ServerName, ConfigStrings.auto_logon_award, "true");
            else
                conf.setConfig(ServerName, ConfigStrings.auto_logon_award, "false");
            if (frm.chk_global_reserve_gold.Checked)
                conf.setConfig(ServerName, ConfigStrings.gold_reserve, frm.num_global_reserve_gold.Value.ToString());
            else conf.setConfig(ServerName, ConfigStrings.gold_reserve, "0");
            if (frm.chk_global_reserve_silver.Checked)
                conf.setConfig(ServerName, ConfigStrings.silver_reserve, frm.num_global_reserve_silver.Value.ToString());
            else conf.setConfig(ServerName, ConfigStrings.silver_reserve, "0");
            if (frm.chk_global_reserve_stone.Checked)
                conf.setConfig(ServerName, ConfigStrings.stone_reserve, frm.num_global_reserve_stone.Value.ToString());
            else conf.setConfig(ServerName, ConfigStrings.stone_reserve, "0");
            if (frm.chk_global_reserve_credit.Checked)
                conf.setConfig(ServerName, ConfigStrings.credit_reserve, frm.num_global_reserve_credit.Value.ToString());
            else conf.setConfig(ServerName, ConfigStrings.credit_reserve, "0");
            conf.setConfig(ServerName, ConfigStrings.gem_price, frm.num_global_gem_price.Value.ToString());

            // key hook
            bool boss_enabled = frm.chk_global_boss_key_enable.Checked;
            bool ctrl = frm.chk_global_boss_key_ctrl.Checked;
            bool alt = frm.chk_global_boss_key_alt.Checked;
            bool shift = frm.chk_global_boss_key_shift.Checked;
            String key = "";
            if (frm.combo_global_boss_key_key.SelectedIndex >= 0) key = frm.combo_global_boss_key_key.SelectedItem.ToString();
            conf.setConfig(ServerName, ConfigStrings.boss_enable, boss_enabled.ToString());
            conf.setConfig(ServerName, ConfigStrings.boss_shift, shift.ToString());
            conf.setConfig(ServerName, ConfigStrings.boss_ctrl, ctrl.ToString());
            conf.setConfig(ServerName, ConfigStrings.boss_alt, alt.ToString());
            conf.setConfig(ServerName, ConfigStrings.boss_key, key);
            if (boss_enabled && key != "")
            {
                if (Enum.IsDefined(typeof(Keys), key))
                {
                    Keys o = (Keys)Enum.Parse(typeof(Keys), key);
                    frm.setKeyHook(shift, ctrl, alt, o);
                }
            }
            else frm.removeKeyHook();
        }
        public override void renderSettings()
        {
            MainForm frm = _mainForm;
            frm.refreshPlayerInfo();
            // save Global settings
            if (frm.getConfig() == null) return;
            Dictionary<string, string> conf = getConfig(ServerName);
            if (conf.ContainsKey(ConfigStrings.auto_logon_award) && conf[ConfigStrings.auto_logon_award] != "")
            {
                frm.chk_global_logonAward.Checked = conf[ConfigStrings.auto_logon_award].ToLower().Equals("true");
            }
            int reserve = 0;
            if (conf.ContainsKey(ConfigStrings.gold_reserve) && conf[ConfigStrings.gold_reserve] != "")
            {
                Int32.TryParse(conf[ConfigStrings.gold_reserve], out reserve);
                if (reserve > 0)
                {
                    frm.chk_global_reserve_gold.Checked = true;
                    frm.num_global_reserve_gold.Value = reserve;
                }
            }
            if (conf.ContainsKey(ConfigStrings.silver_reserve) && conf[ConfigStrings.silver_reserve] != "")
            {
                Int32.TryParse(conf[ConfigStrings.silver_reserve], out reserve);
                if (reserve > 0)
                {
                    frm.chk_global_reserve_silver.Checked = true;
                    frm.num_global_reserve_silver.Value = reserve;
                }
            }
            if (conf.ContainsKey(ConfigStrings.stone_reserve) && conf[ConfigStrings.stone_reserve] != "")
            {
                Int32.TryParse(conf[ConfigStrings.stone_reserve], out reserve);
                if (reserve > 0)
                {
                    frm.chk_global_reserve_stone.Checked = true;
                    frm.num_global_reserve_stone.Value = reserve;
                }
            }
            if (conf.ContainsKey(ConfigStrings.credit_reserve) && conf[ConfigStrings.credit_reserve] != "")
            {
                Int32.TryParse(conf[ConfigStrings.credit_reserve], out reserve);
                if (reserve > 0)
                {
                    frm.chk_global_reserve_credit.Checked = true;
                    frm.num_global_reserve_credit.Value = reserve;
                }
            }
            if (conf.ContainsKey(ConfigStrings.gem_price) && conf[ConfigStrings.gem_price] != "")
            {
                Int32.TryParse(conf[ConfigStrings.gem_price], out reserve);
                if (reserve > 0)
                {
                    frm.num_global_gem_price.Value = reserve;
                }
            }

            // boss key
            bool boss_enabled = conf.ContainsKey(ConfigStrings.boss_enable) && conf[ConfigStrings.boss_enable].ToLower().Equals("true");
            frm.chk_global_boss_key_enable.Checked = boss_enabled;
            bool shift = conf.ContainsKey(ConfigStrings.boss_shift) && conf[ConfigStrings.boss_shift].ToLower().Equals("true");
            frm.chk_global_boss_key_shift.Checked = shift;
            bool ctrl = conf.ContainsKey(ConfigStrings.boss_ctrl) && conf[ConfigStrings.boss_ctrl].ToLower().Equals("true"); ;
            frm.chk_global_boss_key_ctrl.Checked = ctrl;
            bool alt = conf.ContainsKey(ConfigStrings.boss_alt) && conf[ConfigStrings.boss_alt].ToLower().Equals("true");;
            frm.chk_global_boss_key_alt.Checked = alt;
            string key = "";
            if (conf.ContainsKey(ConfigStrings.boss_key) && conf[ConfigStrings.boss_key] != "")
            {
                key = conf[ConfigStrings.boss_key];
                frm.combo_global_boss_key_key.SelectedItem = key;
            }

            if (boss_enabled && key != "")
            {
                if (Enum.IsDefined(typeof(Keys), key))
                {
                    Keys o = (Keys)Enum.Parse(typeof(Keys), key);
                    frm.setKeyHook(shift, ctrl, alt, o);
                }
            }
            else frm.removeKeyHook();
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.auto_logon_award, "true");
            conf.setConfig(ServerName, ConfigStrings.gold_reserve, "200");
            conf.setConfig(ServerName, ConfigStrings.silver_reserve, "100000");
            conf.setConfig(ServerName, ConfigStrings.stone_reserve, "500");
            conf.setConfig(ServerName, ConfigStrings.credit_reserve, "20000");
            conf.setConfig(ServerName, ConfigStrings.gem_price, "50");
            conf.setConfig(ServerName, ConfigStrings.boss_enable, "false");
            conf.setConfig(ServerName, ConfigStrings.boss_shift, "false");
            conf.setConfig(ServerName, ConfigStrings.boss_ctrl, "false");
            conf.setConfig(ServerName, ConfigStrings.boss_alt, "false");
            conf.setConfig(ServerName, ConfigStrings.boss_key, "");
            renderSettings();
        }
    }
}
